package adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.sxpjyy.monkey.mobilenurse.R;

import java.util.ArrayList;
import java.util.List;

import objects.OC_YZJH_Detail;

/**
 * Created by Monkey on 2017/6/8.
 * 医嘱管理适配器
 */

public class OrdersManageAdapter extends BaseAdapter {
    private List<OC_YZJH_Detail> mOMList;
    private List<Boolean> isCheckedList;
    private LayoutInflater inflater;
    ViewHolder holder;
    private boolean isSelectAll = false;

    public OrdersManageAdapter(Context context) {
        this.inflater = LayoutInflater.from(context);
    }

    public void setmOMList(List<OC_YZJH_Detail> mOMList) {
        this.mOMList = mOMList;
        isCheckedList = new ArrayList<>();
        for (int i = 0; i < mOMList.size(); i++) {
            isCheckedList.add(false);
        }
    }

    public boolean isSelectAll() {
        return isSelectAll;
    }

//    public int getSelectedCount() {
//        int count = 0;
//        for (int i = 0; i < isCheckedList.size(); i++) {
//            if (isCheckedList.get(i)) {
//                count++;
//            }
//        }
//        return count;
//    }

    @Override
    public int getCount() {
        if (mOMList != null) {
            return mOMList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (getCount() > 0 && mOMList.get(position) != null) {
            return mOMList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_order, parent, false);
            holder.order_JHH = (TextView) convertView.findViewById(R.id.jhh_text);
            holder.order_YPYF = (TextView) convertView.findViewById(R.id.ypyf_text);
            holder.order_SYPC = (TextView) convertView.findViewById(R.id.sypc_text);
            holder.order_YZLX = (TextView) convertView.findViewById(R.id.yzlx_text);
            holder.order_NAME = (TextView) convertView.findViewById(R.id.yzmc_text);
            holder.order_SYSJ = (TextView) convertView.findViewById(R.id.sysj_text);
            holder.order_CB = (CheckBox) convertView.findViewById(R.id.yzjh_checkbox);
            holder.order_JHZT = (TextView) convertView.findViewById(R.id.jhzt_text);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.order_CB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isCheckedList.set(position, isChecked);
            }
        });

        if (mOMList.get(position) != null) {
            holder.order_CB.setChecked(isCheckedList.get(position));
            holder.order_JHH.setText(mOMList.get(position).getJHH());
            holder.order_YPYF.setText(mOMList.get(position).getYFMC());
            holder.order_SYPC.setText(mOMList.get(position).getSYPC());
            String lx = "";
            switch (mOMList.get(position).getLXXH()) {
                case "1":
                    lx = "输液类";
                    break;
                case "2":
                    lx = "注射类";
                    break;
                case "3":
                    lx = "口服类";
                    break;
                case "4":
                    lx = "其他";
                    break;
            }
            holder.order_YZLX.setText(lx);
            holder.order_NAME.setText(mOMList.get(position).getYZMC());
            holder.order_SYSJ.setText(mOMList.get(position).getJHSJ().substring(0, 19));
            String yzzt = "";
            switch (mOMList.get(position).getYZZT()) {
                case "0":
                    yzzt = "未执行";
                    break;
                case "1":
                    yzzt = "已执行";
                    break;
                case "2":
                    yzzt = "执行中";
                    break;
                case "4":
                    yzzt = "暂停中";
                    break;
            }
            holder.order_JHZT.setText(yzzt);
//            holder.order_JHZT.setText(mOMList.get(position).getYZZT());
        }
        return convertView;
    }

    private class ViewHolder {
        TextView order_JHH;
        TextView order_YPYF;
        TextView order_SYPC;
        TextView order_YZLX;
        TextView order_NAME;
        TextView order_SYSJ;
        TextView order_JHZT;
        CheckBox order_CB;
    }

    public void selectAll(boolean isChecked) {
        for (int i = 0; i < isCheckedList.size(); i++) {
            isCheckedList.set(i, isChecked);
        }
        isSelectAll = isChecked;
    }
}
